import { StringMap } from './types';

// takes a simple map, returns a string
export default function formData(data: StringMap): string {
    return Object
        .keys(data)
        .map((k) => formDataItem(k, typeof (data[k]) === 'string'
            ? data[k].trim()
            : data[k]))
        .filter((str) => typeof str !== 'undefined')
        .join('&');
}

function formDataItem(k: string, v: string | undefined): string | undefined {
    if (typeof v !== 'undefined') {
        return `${k}=${encodeURIComponent(v)}`;
    }
}
